//
//  UserNewTaskCell.m
//  CheekiOS
//
//  Created by mac on 2017/6/30.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "UserNewTaskCell.h"
#import "CCHStepSizeSlider.h"

@interface UserNewTaskCell ()


@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)UILabel *nameLab;
@property (nonatomic, strong)UIButton *taskBtn;
@property (nonatomic, strong)UILabel *describeLab;
@property (nonatomic, strong)CCHStepSizeSlider *slider;
@property (nonatomic, assign)NSIndexPath *indexPath;


@end

@implementation UserNewTaskCell

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath*)indexPath {
    static NSString *UserNewTaskCellIdent= @"UserNewTaskCellIdent";
    UserNewTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:UserNewTaskCellIdent];
    if (!cell) {
        cell = [[UserNewTaskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:UserNewTaskCellIdent];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    return cell;
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.cornerRadius = 5;
        self.bgView.layer.masksToBounds = true;
        [self addSubview:self.bgView];
        
        

        
        
        self.nameLab = [[UILabel alloc] init];
        self.nameLab.textColor = [UIColor blackColor];
        self.nameLab.font = FontP(20);
        self.nameLab.textAlignment = NSTextAlignmentLeft;
        [self.bgView addSubview:self.nameLab];
        
        


        self.describeLab = [[UILabel alloc] init];
        self.describeLab.textColor = ColorI(0x2e353f);
        self.describeLab.font = FontP(15);
        self.describeLab.textAlignment = NSTextAlignmentLeft;
        [self.bgView addSubview:self.describeLab];

        
      
        
        [self layoutView];
        
        
    }
    return self;
}
- (void)layoutView{
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.bottom.equalTo(self).offset(0);
    }];
    
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(20);
        make.left.equalTo(self.bgView).offset(15);
        
    }];
    

    
    [self.describeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLab.mas_bottom).offset(20);
        make.left.equalTo(self.bgView).offset(15);
    }];
    

}
-(void)setModel:(UserNewTaskModel *)Model{
    
    _Model = Model;
    
    for (UIView * subView in self.bgView.subviews){
        if ([subView isKindOfClass:[UIControl class]]) {
            [subView removeFromSuperview];
        }
    }
    self.slider = [[CCHStepSizeSlider alloc] init];
    self.slider.thumbSize = CGSizeMake(20, 20);
    self.slider.sliderOffset = 0;
    self.slider.thumbImage = [UIImage imageNamed:@"p_center_xsrw_round_button"];
    self.slider.AirImage = [UIImage imageNamed:@"p_center_xsrw_button"];
    self.slider.continuous = NO;
    self.slider.type = CCHStepSizeSliderTypeNormal;
    self.slider.backgroundColor = [UIColor clearColor];
    [self.bgView addSubview:self.slider];
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bgView.mas_bottom).offset(+20);
        make.left.equalTo(self.bgView).offset(11);
        make.right.equalTo(self.bgView).offset(-11);
        make.height.mas_equalTo(80);
    }];

    
    self.taskBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.taskBtn.layer.cornerRadius = 5;
    self.taskBtn.layer.masksToBounds = YES;
    [self.bgView addSubview:self.taskBtn];

    [self.taskBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).offset(20);
        make.right.equalTo(self.bgView).offset(-15);
        make.width.mas_equalTo(70);
        
    }];
    
    self.nameLab.text = _Model.remark;
    
    

    
    if ([_Model.status  isEqualToString:@"0"]) {
        [self.taskBtn setTitle:@"去完成" forState:UIControlStateNormal];
        self.taskBtn.titleLabel.font = FontP(15);
        self.taskBtn.backgroundColor = Colorbackground;
        [self.taskBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.taskBtn.enabled = YES;

    }else if ([_Model.status isEqualToString:@"1"]){
        [self.taskBtn setTitle:@"立即领取" forState:UIControlStateNormal];
        self.taskBtn.titleLabel.font = FontP(15);
        self.taskBtn.backgroundColor = Colorbackground;
        [self.taskBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.taskBtn.enabled = YES;

    }else{
        [self.taskBtn setTitle:@"已领取" forState:UIControlStateNormal];
        [self.taskBtn setBackgroundColor:ColorI(0x2e353f)];
        [self.taskBtn setTitleColor:Theme_Color_White forState:UIControlStateNormal];
        self.taskBtn.enabled = NO;
    }
    
    self.taskBtn.tag = _indexPath.row;
    
    [self.taskBtn addTarget:self action:@selector(UserNewTaskButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.describeLab.text = [NSString stringWithFormat:@"完成任务可获得%@钻石",Model.charm];
    
    
    //当前进度
    NSArray *arr = [NSArray array];
    
    if (Model.finish.length) {
        arr = [_Model.finish componentsSeparatedByString:@"/"];
    }
    
    //完成多少
    self.slider.AirLabel.text = _Model.finish;
    self.slider.AirLabel.hidden = NO;
    
    NSString *miniStr = [NSString stringWithFormat:@"%@",arr[0]];
    NSString *maxStr = [NSString stringWithFormat:@"%@",arr[1]];
    


    NSInteger miniValue = [miniStr integerValue];
    NSInteger maxValue = [maxStr integerValue];
    
    //最小
    self.slider.minimumValue = 0;
    //最大
    self.slider.maximumValue = maxValue;
    //进度
    self.slider.value = miniValue;
    
    
    
  

}

- (void)UserNewTaskButtonClick:(UIButton *)button{
    if ([self.delegate respondsToSelector:@selector(UserNewTaskButtonClick:)]) {
        [self.delegate UserNewTaskButtonClick:button];
    }
}

@end
